package com.frozenc.controller.user;

import com.alibaba.fastjson.JSON;
import com.frozenc.Util.CryptUtil;
import com.frozenc.Util.EmailUtil;
import com.frozenc.domain.News;
import com.frozenc.domain.User;
import com.frozenc.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * Created by FrozenC on 2016/9/13.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @ModelAttribute
    public void setUser(HttpSession session, ModelMap modelMap){
        User user = (User) session.getAttribute("currUser");
        modelMap.addAttribute("currUser",user);
        return ;
    }

    @RequestMapping(value = "/info",method = RequestMethod.GET)
    @ResponseBody
    private String getInfo(ModelMap modelMap){
        User originUser = (User) modelMap.get("currUser");

        if(originUser == null){
            return JSON.toJSONString(null);
        }

        User copy = new User();
        BeanUtils.copyProperties(copy, originUser);
        copy.setPassword(null);

        return JSON.toJSONString(copy);
    }

    @RequestMapping(value = "/info",method = RequestMethod.PUT)
    @ResponseBody
    private String updateInfo(@RequestBody User user,ModelMap modelMap,HttpSession httpSession){
        User originUser = (User) modelMap.get("currUser");
        System.out.println(originUser);
        if(originUser==null || !originUser.getUsername().equals(user.getUsername())){
            return "未登录或者错误的用户名";
        }

        if(!EmailUtil.isEmail(user.getEmail())){
            return "邮箱格式错误";
        }

        String password = user.getPassword();
        if(!user.getPassword().equals("")){
            if(StringUtils.isEmpty(user.getPassword()) && (password.length()<6 || password.length()>20)){
                return "密码长度至少为6,不能超过20";
            }
        }

        String motto = user.getMotto();
        if(motto!=null && motto.length()>100){
            return "个性签名过长";
        }

        user.setId(originUser.getId());

        boolean needReLogin = false;

        if(StringUtils.isEmpty(user.getPassword()) ){
            user.setPassword(originUser.getPassword());
        }
        else{
            needReLogin=true;
            user.setPassword(CryptUtil.getSHA1(user.getPassword(),originUser.getSalt()));
        }

        if(StringUtils.isEmpty(user.getEmail())){
            user.setEmail(originUser.getEmail());
        }

        int res;
        try {
            res = userMapper.updateInfo(user);
        }catch (DataAccessException e){
            System.out.println(e.getMessage());
            return "未知错误";
        }

        System.out.println(res);

        if(needReLogin){
            httpSession.removeAttribute("currUser");
            return "302";
        }

        return "ok";
    }
}
